package ru.aslanov.schedule.client;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import ru.aslanov.schedule.client.i18n.I18nUtil;
import ru.aslanov.schedule.client.util.GUIUtil;

/**
 * Created by IntelliJ IDEA.
 * Created: Feb 11, 2010 5:22:47 PM
 *
 * @author Sergey Aslanov
 */
public class DataPanel extends VLayout implements Invalidatable {
    private ListGrid listGrid;
    protected HLayout buttons;

    public DataPanel(DataSource dataSource, String title, String width, String height, String scheduleKey, boolean canChange) {
        super(10);
        final ListGrid listGrid = new ListGrid();
        this.listGrid = listGrid;
        this.listGrid.setTitle(title);
        listGrid.setDataSource(dataSource);
        listGrid.setWidth(width);
        listGrid.setHeight(height);
        listGrid.setShowAllRecords(true);
        listGrid.setAutoFetchData(true);
        listGrid.setCanEdit(canChange);
        listGrid.setWrapCells(true);
        listGrid.setFixedRecordHeights(false);
        
        this.addMember(listGrid);

        buttons = new HLayout(5);
        if (canChange) {
            com.smartgwt.client.widgets.Button addButton = new com.smartgwt.client.widgets.Button(I18nUtil.cnt().addButton());
            GUIUtil.addAddHandler(listGrid, addButton, scheduleKey);

            com.smartgwt.client.widgets.Button removeButton = new com.smartgwt.client.widgets.Button(I18nUtil.cnt().removeButton());
            GUIUtil.addRemoveHandler(listGrid, removeButton, null);

            buttons.addMember(addButton);
            buttons.addMember(removeButton);
        }
        this.addMember(buttons);

    }

    public ListGrid getListGrid() {
        return listGrid;
    }

    @Override
    public void invalidateData() {
        listGrid.invalidateCache();
    }
}
